'use strict';

const { DataTypes } = require('sequelize');
const sequelize = require('../../database/sequelize');

const City = sequelize.define(
  'City',
  {
    id: {
      type: DataTypes.INTEGER.UNSIGNED,
      primaryKey: true,
      autoIncrement: true,
      comment: '城市ID',
    },
    name: {
      type: DataTypes.STRING(50),
      allowNull: false,
      unique: true,
      comment: '城市名称',
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: '城市描述',
    },
    image: {
      type: DataTypes.STRING(255),
      allowNull: true,
      comment: '封面图',
    },
    tags: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: '标签(存储JSON字符串)',
    },
    rating: {
      type: DataTypes.FLOAT,
      allowNull: false,
      defaultValue: 0,
      comment: '评分',
    },
    views: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '浏览量',
    },
    recommendations: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '推荐数',
    },
    bestSeason: {
      type: DataTypes.STRING(20),
      allowNull: true,
      comment: '最佳旅行时间',
    },
    recommendedDays: {
      type: DataTypes.STRING(20),
      allowNull: true,
      comment: '推荐天数',
    },
    attractions: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: '热门景点(存储JSON字符串)',
    },
  },
  {
    tableName: 'cities',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    underscored: true,
  }
);

// 初始化表并插入示例数据（仅开发环境）
(async () => {
  try {
    // await City.sync({ alter: true });
    const count = await City.count();
    if (count === 0) {
      await City.bulkCreate([
        {
          name: '成都',
          description: '美食之都，悠闲生活的代表，拥有丰富的历史文化和自然景观。火锅、熊猫和茶馆是这座城市的标志。',
          image: 'https://images.unsplash.com/photo-1563496779217-144e97f6db9b?ixlib=rb-1.2.1&auto=format&fit=crop&w=600&q=80',
          tags: JSON.stringify(['美食', '文化', '熊猫', '休闲']),
          rating: 4.8,
          views: 12500,
          recommendations: 890,
          bestSeason: '春秋季',
          recommendedDays: '3-5天',
          attractions: JSON.stringify(['宽窄巷子', '锦里', '大熊猫基地', '都江堰'])
        },
        {
          name: '西安',
          description: '千年古都，兵马俑的故乡，丝绸之路的起点。这里汇聚了丰富的历史遗迹和特色美食。',
          image: 'https://images.unsplash.com/photo-1547981609-4b6bf67b7d12?ixlib=rb-1.2.1&auto=format&fit=crop&w=600&q=80',
          tags: JSON.stringify(['历史', '文化', '美食', '古迹']),
          rating: 4.7,
          views: 9800,
          recommendations: 765,
          bestSeason: '春秋季',
          recommendedDays: '4-6天',
          attractions: JSON.stringify(['兵马俑', '大雁塔', '古城墙', '回民街'])
        },
        {
          name: '杭州',
          description: '人间天堂，西湖美景，龙井茶的故乡。融合了自然风光与人文历史的完美结合。',
          image: 'https://images.unsplash.com/photo-1548960556-7ec3a5cb719d?ixlib=rb-1.2.1&auto=format&fit=crop&w=600&q=80',
          tags: JSON.stringify(['自然', '文化', '美食', '湖泊']),
          rating: 4.6,
          views: 11200,
          recommendations: 920,
          bestSeason: '春季',
          recommendedDays: '2-4天',
          attractions: JSON.stringify(['西湖', '灵隐寺', '龙井村', '宋城'])
        },
        {
          name: '厦门',
          description: '海上花园，鼓浪屿的浪漫，闽南文化的代表。温暖的气候和美丽的海滩让人流连忘返。',
          image: 'https://images.unsplash.com/photo-1531798122087-12e4ed3d2d86?ixlib=rb-1.2.1&auto=format&fit=crop&w=600&q=80',
          tags: JSON.stringify(['海岛', '美食', '文化', '浪漫']),
          rating: 4.5,
          views: 8700,
          recommendations: 654,
          bestSeason: '秋冬季',
          recommendedDays: '3-4天',
          attractions: JSON.stringify(['鼓浪屿', '厦门大学', '环岛路', '曾厝垵'])
        },
        {
          name: '丽江',
          description: '古城风情，纳西文化，玉龙雪山的壮丽。这里是寻找宁静与自然美景的理想之地。',
          image: 'https://images.unsplash.com/photo-1599733950404-2c46d3e5c5b6?ixlib=rb-1.2.1&auto=format&fit=crop&w=600&q=80',
          tags: JSON.stringify(['古城', '自然', '文化', '雪山']),
          rating: 4.7,
          views: 7600,
          recommendations: 543,
          bestSeason: '春秋季',
          recommendedDays: '4-5天',
          attractions: JSON.stringify(['丽江古城', '玉龙雪山', '束河古镇', '蓝月谷'])
        },
        {
          name: '重庆',
          description: '山城雾都，火锅之都，8D魔幻城市的魅力。独特的城市地貌和美食文化令人难忘。',
          image: 'https://images.unsplash.com/photo-1599396170196-5d5f84a2e7c5?ixlib=rb-1.2.1&auto=format&fit=crop&w=600&q=80',
          tags: JSON.stringify(['美食', '夜景', '山城', '魔幻']),
          rating: 4.6,
          views: 10500,
          recommendations: 789,
          bestSeason: '春秋季',
          recommendedDays: '3-4天',
          attractions: JSON.stringify(['洪崖洞', '解放碑', '磁器口', '长江索道'])
        }
      ]);
      console.log('✅ 已插入示例城市数据');
    }
  } catch (e) {
    console.error('City模型初始化失败:', e);
  }
})();

module.exports = City;